/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.executableTasks.GoogleMapsRetriever;

public class MapView extends Form implements CommandListener, IView {
	private ApplicationController controller;
	private Command reset ;
	private Command zoomIn;
	private Command zoomOut;
	private Command mapTypeNormal;
	private Command mapTypeSatellite;
	private Command mapTypeTerrain;
	private Command mapTypeHybrid;
	
	private String mapType;
	private int zoomLevel = -1;
	private static final int DEFAULT_ZOOMLEVEL = 13;
	
	public MapView() {
		super("Map View");
		initialize();
		createCommands();
		this.setCommandListener(this);
	}

	private void initialize() {
		this.mapType = GoogleMapsRetriever.MAP_TYPE_NORMAL;
	}

	/**
	 * Creates the commands
	 */
	private void createCommands() {
		reset = new Command("Reset", "Reset Map", Command.SCREEN, 0);
		zoomIn = new Command("Zoom in", "Zoom in", Command.SCREEN,0);
		zoomOut = new Command("Zoom out", "Zoom out", Command.SCREEN,0);
		
		//map types
		mapTypeNormal = new Command("Type normal", "Type normal", Command.SCREEN,0);
		mapTypeSatellite = new Command("Type satellite", "Type satellite", Command.SCREEN,0);
		mapTypeTerrain = new Command("Type terrain", "Type terrain", Command.SCREEN,0);
		mapTypeHybrid = new Command("Type hybrid", "Type hybrid", Command.SCREEN,0);
		
		this.addCommand(reset);
		this.addCommand(zoomIn);
		this.addCommand(zoomOut);
		this.addCommand(mapTypeNormal);
		this.addCommand(mapTypeSatellite);
		this.addCommand(mapTypeTerrain);
		this.addCommand(mapTypeHybrid);

	}

	public void showMap(Image mapImage) {
		this.deleteAll();
		this.append(mapImage);
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.BACK) {
			ScreenController.getInstance().last();
		} else if(c == reset) {
			this.zoomLevel = 14;
			this.mapType = GoogleMapsRetriever.MAP_TYPE_NORMAL;
			controller.executeGoogleMapsRequest(false);
		} else if(c == zoomIn){
			if(zoomLevel == -1) zoomLevel = DEFAULT_ZOOMLEVEL;
			this.zoomLevel++;
			this.zoomLevel = (this.zoomLevel < 0) ? 1 : this.zoomLevel;
			this.zoomLevel = (this.zoomLevel > 19) ? 19 : this.zoomLevel;
			controller.executeGoogleMapsRequest(false);
		} else if(c == zoomOut){
			if(zoomLevel == -1) zoomLevel = DEFAULT_ZOOMLEVEL;
			this.zoomLevel--;
			this.zoomLevel = (this.zoomLevel < 0) ? 1 : this.zoomLevel;
			this.zoomLevel = (this.zoomLevel > 19) ? 19 : this.zoomLevel;
			controller.executeGoogleMapsRequest(false);
		} else if(c == mapTypeNormal){
			this.mapType = GoogleMapsRetriever.MAP_TYPE_NORMAL;
			controller.executeGoogleMapsRequest(false);
		}else if(c == mapTypeSatellite){
			this.mapType = GoogleMapsRetriever.MAP_TYPE_SATELLITE;
			controller.executeGoogleMapsRequest(false);
		}else if(c == mapTypeTerrain){
			this.mapType = GoogleMapsRetriever.MAP_TYPE_TERRAIN;
			controller.executeGoogleMapsRequest(false);
		}else if(c == mapTypeHybrid){
			this.mapType = GoogleMapsRetriever.MAP_TYPE_HYBRID;
			controller.executeGoogleMapsRequest(false);
		}
	}
	
	public String getMapType() {
		return mapType;
	}

	public int getZoomLevel() {
		return zoomLevel;
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;
	}
	
}
